﻿namespace HiMap.Carto
{
    using HiMap.Base;
    using HiMap.Geometry;
    using System;
    using System.Text;

    public class FeatureClassClass : ClassClass, IFeatureClass,/* IClass ,*/ IHiMapPtr
    {
        public FeatureClassClass(int InPtr, bool Used)
        {
            base.m_InPtr = InPtr;
            base.m_Used = Used;
            
        }

        bool IFeatureClass.BeginEdit()
        {
            return HiMap.Carto.VC.KWG_FEATURELAYER_EDIT_BEGIN(base.m_InPtr);
        }

        bool IFeatureClass.Compress()
        {
            return HiMap.Carto.VC.KWG_FEATURELAYER_COMPRESS(base.m_InPtr);
        }

        IFeature IFeatureClass.CreateFeature()
        {
            if (!HiMap.Carto.VC.KWG_FEATURELAYER_IS_EDIT(base.m_InPtr))
            {
                return null;
            }
            return new FeatureClass(this);
        }

        bool IFeatureClass.DeleteFeature(IFeature obj)
        {
            if (!HiMap.Carto.VC.KWG_FEATURELAYER_IS_EDIT(base.m_InPtr))
            {
                return false;
            }
            FeatureClass class2 = (FeatureClass) obj;
            HiMap.Carto.VC.KWG_FEATURELAYER_DELETE_FEATURE(base.m_InPtr, class2.DataIndex);
            obj = null;
            return true;
        }

        bool IFeatureClass.IsEdit()
        {
            return HiMap.Carto.VC.KWG_FEATURELAYER_IS_EDIT(base.m_InPtr);
        }

        bool IFeatureClass.OpenDataIndex(DataIndexType type, bool NoneCreate)
        {
            int num = HiMap.Carto.VC.KWG_KDI_OPEN(new StringBuilder(this.Name + ".kdi"), (int) type, NoneCreate);
            if (num == 0)
            {
                return false;
            }
            HiMap.Carto.VC.KWG_FEATURELAYER_SET_DATA_INDEX_ENGINE(base.m_InPtr, num);
            return true;
        }

        bool IFeatureClass.OpenSpatialIndex(bool CreateNew)
        {
            string name = this.Name;
            if (CreateNew)
            {
                int num = HiMap.Carto.VC.KWG_SPATIAL_CREATE(base.m_InPtr, new StringBuilder(name + ".ksi"));
                if (num == 0)
                {
                    return false;
                }
                HiMap.Carto.VC.KWG_FEATURELAYER_SET_SPATIAL_INDEX_ENGINE(base.m_InPtr, num);
                return true;
            }
            int num2 = HiMap.Carto.VC.KWG_SPATIAL_OPEN(base.m_InPtr, new StringBuilder(name + ".ksi"));
            if (num2 == 0)
            {
                num2 = HiMap.Carto.VC.KWG_SPATIAL_CREATE(base.m_InPtr, new StringBuilder(name + ".ksi"));
            }
            if (num2 == 0)
            {
                return false;
            }
            HiMap.Carto.VC.KWG_FEATURELAYER_SET_SPATIAL_INDEX_ENGINE(base.m_InPtr, num2);
            return true;
        }

        bool IFeatureClass.SaveEdit()
        {
            bool flag = HiMap.Carto.VC.KWG_FEATURELAYER_EDIT_SAVE(base.m_InPtr);
            HiMap.Carto.VC.KWG_FEATURELAYER_IS_EDIT(base.m_InPtr);
            return flag;
        }

        IFeatureCursor IFeatureClass.Search(IQueryFilter Filter)
        {
            return new FeatureCursorClass(this, Filter);
        }

        bool IFeatureClass.StopEdit(bool save)
        {
            return HiMap.Carto.VC.KWG_FEATURELAYER_EDIT_STOP(base.m_InPtr, save);
        }

        FeatureClassType IFeatureClass.FeatureClassType
        {
            get
            {
                return (FeatureClassType) HiMap.Carto.VC.KWG_FEATURELAYER_GET_TYPE(base.m_InPtr);
            }
        }

        IFields IFeatureClass.Fields
        {
            get
            {
                return new FieldsClass(this);
            }
        }

        GeometryType IFeatureClass.GeometryType
        {
            get
            {
                switch (HiMap.Carto.VC.KWG_FEATURELAYER_GET_GEOMETRYTYPE(base.m_InPtr))
                {
                    case 0x15:
                    case 1:
                    case 11:
                        return GeometryType.Point;

                    case 0x17:
                    case 3:
                    case 13:
                        return GeometryType.Polyline;

                    case 0x19:
                    case 5:
                    case 15:
                        return GeometryType.Polygon;

                    case 0x1c:
                    case 0x12:
                    case 8:
                        return GeometryType.MultiPoint;
                }
                return GeometryType.NotGeometry;
            }
        }
    }
}

